<?php

namespace common\models\fenhuicat;

use Yii;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use common\models\fenhuigrade\Fenhuigrade;

/**
 * This is the model class for table "fenhuicat".
 *
 * @property string $id
 * @property string $name
 * @property integer $status
 * @property string $type
 * @property string $ctype
 * @property string $url
 * @property integer $sort
 * @property string $seo_keywords
 * @property string $seo_contents
 * @property integer $parent_id
 * @property string $fg_id
 */
class Fenhuicat extends \yii\db\ActiveRecord
{
    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;
    private $_statusLabel;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'fenhuicat';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['status', 'sort', 'parent_id'], 'integer'],
            [['seo_contents'], 'string'],
            [['name', 'type', 'ctype', 'url', 'seo_keywords', 'fg_id'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'name' => Yii::t('app', '栏目名称'),
            'status' => Yii::t('app', '导航显示'),
            'type' => Yii::t('app', '栏目模板'),
            'ctype' => Yii::t('app', '内容模板'),
            'url' => Yii::t('app', '外部链接'),
            'sort' => Yii::t('app', '排序'),
            'seo_keywords' => Yii::t('app', '关键词'),
            'seo_contents' => Yii::t('app', '描述语'),
            'parent_id' => Yii::t('app', '父栏目ID'),
            'fg_id' => Yii::t('app', '分会名称'),
        ];
    }
    public function getFenhuigraed()
    {
        return $this->hasOne(Fenhuigrade::className(), ['id' => 'category_id']);
    }

    public static function getArrayFenhuigrade()
    {
        return ArrayHelper::map(Fenhuigrade::find()->where(['status'=>10])->all(), 'id', 'name');
    }

    /**
     * 栏目分类
     */


    public static function get($parentId = 0, $array = array(), $level = 0, $add = 2, $repeat = '　')
    {
        $strRepeat = '';
        // add some spaces or symbols for non top level categories
        if ($level>1) {
            for($j = 0; $j < $level; $j ++)
            {
                $strRepeat .= $repeat;
            }
        }
        if($level>0)
            $strRepeat .= '';

        $newArray = array ();
        $tempArray = array ();
        foreach ( ( array ) $array as $v )
        {
//            $fgid=$v['id'];
//            $data = Yii::$app->db->createCommand("SELECT fenhuigrade.name as title  FROM fenhuigrade  left join fenhuicat  on fenhuicat.fg_id = fenhuigrade.id  where  fenhuigrade.id = $fgid")
//                ->queryOne();
            if ($v['parent_id'] == $parentId)
            {
                $newArray [] = array ('id' => $v['id'], 'name' => $v['name'], 'parent_id' => $v['parent_id'], 'str_label' => $strRepeat.$v['name'],'status'=>$v['status'],'fg_id'=>$v['fg_id']);

                $tempArray = self::get ( $v['id'], $array, ($level + $add), $add, $repeat);
                if ($tempArray)
                {
                    $newArray = array_merge ( $newArray, $tempArray );
                }
            }
        }
        return $newArray;
    }

    public static function gets($parentId = 0, $array = array(), $level = 0, $add = 2, $repeat = '　')
    {
        $strRepeat = '';
        // add some spaces or symbols for non top level categories
        if ($level>1) {
            for($j = 0; $j < $level; $j ++)
            {
                $strRepeat .= $repeat;
            }
        }
        if($level>0)
            $strRepeat .= '';

        $newArray = array ();
        $tempArray = array ();
        foreach ( ( array ) $array as $v )
        {
            if ($v['parent_id'] == $parentId)
            {

                echo Html::tag('option', Html::encode($v['name']), array('value' =>$v['id']));
                $newArray [] = array ('id' => $v['id'], 'name' => $v['name'], 'parent_id' => $v['parent_id'], 'str_label' => $strRepeat.$v['name'],'status'=>$v['status']);

                $tempArray = self::get ( $v['id'], $array, ($level + $add), $add, $repeat);
                if ($tempArray)
                {
                    $newArray = array_merge ( $newArray, $tempArray );
                }
            }
        }
        return $newArray;
    }
  

    public function getStatusLabel()
    {
        if ($this->_statusLabel === null) {
            $statuses = self::getArrayStatus();
            $this->_statusLabel = $statuses[$this->status];
        }
        return $this->_statusLabel;
    }
    /**
     * @inheritdoc
     */
    public static function getArrayStatus()
    {
        return [

            self::STATUS_ACTIVE => Yii::t('app', '是'),
            self::STATUS_INACTIVE => Yii::t('app', '否'),
        ];
    }

    public static function isDel($id)
    {
        if (((self::find()->where(['parent_id'=>$id])->one()) != null)) {
            return true;
        } else {
            return false;
        }
    }
}
